/*
 * Copyright (c) Huawei Device Co., Ltd. 2023. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ef_parser.h"
#include "der_parser.h"

#include <gtest/gtest.h>
#include <memory>

#include "channel.h"
#include "ipc_object_stub.h"
#include "loghelper.h"
#include "reader.h"
#include "se_service.h"
#include "service_manager.h"
#include "access_control_enforcer.h"
#include "secure_element_agent.h"
#include "access_rule_cache.h"
#include "ef_ac_conditions.h"
#include "ef_ac_main.h"
#include "ef_ac_rules.h"
#include "ef_dodf.h"
#include "ef_odf.h"
#include "arf_manager.h"

namespace OHOS {
namespace SE {
namespace TEST {
using namespace testing::ext;
using namespace OHOS::SE;

class DerParserTest : public testing::Test {
public:
    static void SetUpTestCase() {};
    static void TearDownTestCase() {};
    void SetUp();
    void TearDown();
};

void DerParserTest::SetUp()
{
}

void DerParserTest::TearDown()
{
}

/**
 * @tc.name: IsParsingEnd_Test001
 * @tc.desc: Test IsParsingEnd.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, IsParsingEnd_Test001, TestSize.Level1)
{
    std::vector<uint8_t> buffer;
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    bool ret = derParser->IsParsingEnd();
    EXPECT_TRUE(ret);
}

/**
 * @tc.name: IsParsingEnd_Test002
 * @tc.desc: Test IsParsingEnd.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, IsParsingEnd_Test002, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0x01, 0x02, 0x03};
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    bool ret = derParser->IsParsingEnd();
    EXPECT_TRUE(!ret);
}

/**
 * @tc.name: IsParsingEnd_Test003
 * @tc.desc: Test IsParsingEnd.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, IsParsingEnd_Test003, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0xFF, 0x02, 0x03};
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    bool ret = derParser->IsParsingEnd();
    EXPECT_TRUE(!ret);
}

/**
 * @tc.name: IsParsingEnd_Test004
 * @tc.desc: Test IsParsingEnd.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, IsParsingEnd_Test004, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0xFF, 0xFF, 0x03};
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    bool ret = derParser->IsParsingEnd();
    EXPECT_TRUE(!ret);
}

/**
 * @tc.name: GetTlvType_Test001
 * @tc.desc: Test GetTlvType.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, GetTlvType_Test001, TestSize.Level1)
{
    std::vector<uint8_t> buffer;
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    uint8_t ret = derParser->GetTlvType();
    EXPECT_TRUE(ret != 0xFF);
}

/**
 * @tc.name: GetTlvType_Test002
 * @tc.desc: DerParserTest GetTlvType.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, GetTlvType_Test002, TestSize.Level1)
{
    std::string dataStr = "621B8202412183025031A5038001618A01058B036F06118002001088009000";
    std::vector<uint8_t> dataVec;
    CommonUtils::HexStringToBytesArray(dataStr, dataVec);
    std::shared_ptr<DerParser> der = std::make_shared<DerParser>(dataVec);
    if (der == nullptr) {
        ErrorLog("der nullptr.");
        return;
    }
    uint8_t type = der->GetTlvType();
    EXPECT_TRUE(type == 0x62);
}

/**
 * @tc.name: GetTlvData_Test001
 * @tc.desc: Test GetTlvData.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, GetTlvData_Test001, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0x01, 0x02, 0x03};
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    std::vector<uint8_t> ret = derParser->GetTlvData();
    EXPECT_TRUE(ret.size() != 0);
}

/**
 * @tc.name: RecoverParsingStatus_Test001
 * @tc.desc: Test RecoverParsingStatus.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, RecoverParsingStatus_Test001, TestSize.Level1)
{
    std::vector<uint8_t> buffer;
    uint8_t currentIndex = 0;
    uint8_t tlvDataLength = 0;
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    derParser->RecoverParsingStatus(currentIndex, tlvDataLength);
    EXPECT_TRUE(derParser != nullptr);
}

/**
 * @tc.name: RecoverParsingStatus_Test002
 * @tc.desc: Test RecoverParsingStatus.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, RecoverParsingStatus_Test002, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0x01};
    uint8_t currentIndex = 0x01;
    uint8_t tlvDataLength = 0x02;
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    derParser->RecoverParsingStatus(currentIndex, tlvDataLength);
    EXPECT_TRUE(derParser != nullptr);
}

/**
 * @tc.name: RecoverParsingStatus_Test003
 * @tc.desc: Test RecoverParsingStatus.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, RecoverParsingStatus_Test003, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0x01};
    uint8_t currentIndex = 0xFF;
    uint8_t tlvDataLength = 0x00;
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    derParser->RecoverParsingStatus(currentIndex, tlvDataLength);
    EXPECT_TRUE(derParser != nullptr);
}

/**
 * @tc.name: RecoverParsingStatus_Test004
 * @tc.desc: Test RecoverParsingStatus.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, RecoverParsingStatus_Test004, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0x01};
    uint8_t currentIndex = 0xFF;
    uint8_t tlvDataLength = 0x02;
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    derParser->RecoverParsingStatus(currentIndex, tlvDataLength);
    EXPECT_TRUE(derParser != nullptr);
}

/**
 * @tc.name: RecoverParsingStatus_Test005
 * @tc.desc: Test RecoverParsingStatus.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, RecoverParsingStatus_Test005, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0x01};
    uint8_t currentIndex = 0x01;
    uint8_t tlvDataLength = 0x00;
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    derParser->RecoverParsingStatus(currentIndex, tlvDataLength);
    EXPECT_TRUE(derParser != nullptr);
}

/**
 * @tc.name: ParseDodfOid_Test005
 * @tc.desc: Test ParseDodfOid.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, ParseDodfOid_Test005, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0x01};
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    derParser->ParseDodfOid();
    EXPECT_TRUE(derParser != nullptr);
}

/**
 * @tc.name: ParseEfPath_Test001
 * @tc.desc: Test ParseEfPath.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, ParseEfPath_Test001, TestSize.Level1)
{
    std::vector<uint8_t> buffer = {0x01};
    std::shared_ptr<DerParser> derParser = std::make_shared<DerParser>(buffer);
    derParser->ParseEfPath();
    EXPECT_TRUE(derParser != nullptr);
}

/**
 * @tc.name: ParseAcConditionFileData_Test001
 * @tc.desc: Test ParseAcConditionFileData.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, ParseAcConditionFileData_Test001, TestSize.Level1)
{
    std::weak_ptr<SecureElementAgent> seAgent;
    ArfManager::GetInstance().Initialize(seAgent);
    std::vector<uint8_t> rawData;
    uint32_t tag = 0;
    std::shared_ptr<AidRefDo> aidRefDo = std::make_shared<AidRefDo>(rawData, tag);
    std::vector<uint8_t> data;
    std::shared_ptr<EfAcConditions> efAcConditions = std::make_shared<EfAcConditions>(aidRefDo);
    efAcConditions->ParseAcConditionFileData(data);
    EXPECT_TRUE(efAcConditions != nullptr);
}

/**
 * @tc.name: ParseAcConditionFileData_Test002
 * @tc.desc: Test ParseAcConditionFileData.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, ParseAcConditionFileData_Test002, TestSize.Level1)
{
    std::weak_ptr<SecureElementAgent> seAgent;
    ArfManager::GetInstance().Initialize(seAgent);
    std::vector<uint8_t> rawData;
    uint32_t tag = 0;
    std::shared_ptr<AidRefDo> aidRefDo = std::make_shared<AidRefDo>(rawData, tag);
    std::vector<uint8_t> path;
    std::shared_ptr<EfAcConditions> efAcConditions = std::make_shared<EfAcConditions>(aidRefDo);
    efAcConditions->ParseAcConditionFileData(path);
    EXPECT_TRUE(efAcConditions != nullptr);
}

/**
 * @tc.name: ParseAcMainFileByPath_Test001
 * @tc.desc: Test ParseAcMainFileByPath.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, ParseAcMainFileByPath_Test001, TestSize.Level1)
{
    std::weak_ptr<SecureElementAgent> seAgent;
    ArfManager::GetInstance().Initialize(seAgent);
    ArfManager::GetInstance().SetUsedSeName(SE_NAME_SIM);
    std::vector<uint8_t> path = {0x00, 0x48, 0x01, 0x02};
    std::shared_ptr<EfAcMain> efAcMain = std::make_shared<EfAcMain>(path);
    efAcMain->ParseAcMainFileByPath();
    EXPECT_TRUE(efAcMain != nullptr);
}

/**
 * @tc.name: ParseAcRulesFileByPath_Test001
 * @tc.desc: Test ParseAcRulesFileByPath.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, ParseAcRulesFileByPath_Test001, TestSize.Level1)
{
    std::weak_ptr<SecureElementAgent> seAgent;
    ArfManager::GetInstance().Initialize(seAgent);
    ArfManager::GetInstance().SetUsedSeName(SE_NAME_SIM);
    std::vector<uint8_t> path = {0x00, 0x48, 0x01, 0x02};
    std::shared_ptr<EfAcRules> efAcRules = std::make_shared<EfAcRules>();
    efAcRules->ParseAcRulesFileByPath(path);
    EXPECT_TRUE(efAcRules != nullptr);
}

/**
 * @tc.name: ReadDodfFileByPath_Test001
 * @tc.desc: Test ReadDodfFileByPath.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, ReadDodfFileByPath_Test001, TestSize.Level1)
{
    std::weak_ptr<SecureElementAgent> seAgent;
    ArfManager::GetInstance().Initialize(seAgent);
    ArfManager::GetInstance().SetUsedSeName(SE_NAME_SIM);
    std::vector<uint8_t> path = {0x00, 0x48, 0x01, 0x02};
    std::shared_ptr<EfDodf> efDodf = std::make_shared<EfDodf>();
    efDodf->ReadDodfFileByPath(path);
    EXPECT_TRUE(efDodf != nullptr);
}

/**
 * @tc.name: ReadOdfFileByPath_Test001
 * @tc.desc: Test ReadOdfFileByPath.
 * @tc.type: FUNC
 */
HWTEST_F(DerParserTest, ReadOdfFileByPath_Test001, TestSize.Level1)
{
    std::weak_ptr<SecureElementAgent> seAgent;
    ArfManager::GetInstance().Initialize(seAgent);
    ArfManager::GetInstance().SetUsedSeName(SE_NAME_SIM);
    std::vector<uint8_t> path = {0x00, 0x48, 0x01, 0x02};
    std::shared_ptr<EfOdf> efOdf = std::make_shared<EfOdf>();
    efOdf->ReadOdfFileByPath(path);
    EXPECT_TRUE(efOdf != nullptr);
}
} // TEST
} // SE
} // OHOS